/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mmaingenieria.GestorFacturacion.database;

import com.mmaingenieria.GestorFacturacion.MySQL.MySqlUsuarioDAO;
import com.mmaingenieria.GestorFacturacion.interfaces.InterfaceUsuarioDAO;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

/**
 *
 * @author Mikle
 */
public class MySqlDAOFactory extends DAOFactory{
    //inicializo un DataSource (para utilizar el context.xml
    private static DataSource ds = null;
    
    //Constructor
    //************************************************************************
    public MySqlDAOFactory(){
        try {
            Context ictx = new InitialContext();
            Context ctx = (Context) ictx.lookup("java:comp/env");
            ds = (DataSource) ctx.lookup("jdbc/GestorFacturacion");
        } catch (NamingException ex) {
            Logger.getLogger(MySqlDAOFactory.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    //Metodos
    //************************************************************************
    
    //Conectar con la base de datos
    public static Connection conectar(){
        Connection conexion = null; //Creamos un tipo connection
        try {
            conexion = ds.getConnection();//Hago la conexsion con los datos del context
        } catch (Exception ex) {
            Logger.getLogger(MySqlDAOFactory.class.getName()).log(Level.SEVERE, null, ex);
        }
        return conexion;
    }
    
    
    //Desconectar con la base de datos
    public static void desconectar(Connection conexion) {
        if(conexion!=null){
            try {
                conexion.close();
            } catch (SQLException ex) {
                Logger.getLogger(MySqlDAOFactory.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
    
    
    //Metodos que tengo que implementar al extender de DAOFActory
    //*************************************************************************
    @Override
    public InterfaceUsuarioDAO getInterfaceUsuarioDAO() {
        return new MySqlUsuarioDAO();
    }
    
    
    
}
